package cn.mumzone.pos.ui.fragment.warehouse;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;

import butterknife.BindView;
import cn.mumzone.basic.base.BaseFragment;
import cn.mumzone.basic.net.response.PageListResponse;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.basic.util.PosToast;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.InWarehouseItemEntity;
import cn.mumzone.pos.presenter.WarehouseItemPresenter;
import cn.mumzone.pos.presenter.contract.WarehouseItemContract;
import cn.mumzone.pos.ui.activity.ACT_WarehouseDetail;
import cn.mumzone.pos.ui.adapter.warehouse.ADA_WarehouseItem;
import cn.mumzone.pos.ui.widgets.refresh.SmartRefreshLayout;
import cn.mumzone.pos.ui.widgets.refresh.api.RefreshLayout;
import cn.mumzone.pos.ui.widgets.refresh.constant.RefreshState;
import cn.mumzone.pos.ui.widgets.refresh.listener.OnRefreshListener;
import cn.mumzone.pos.util.DateHolder;

/**
 * Created By Dlx
 * on 2019/9/25
 */
public class FRA_WarehouseItem extends BaseFragment<WarehouseItemPresenter> implements WarehouseItemContract.View, ADA_WarehouseItem.OnCardClickListener, OnRefreshListener, OnLoadMoreListener {
    public static final int TYPE_ALL = 7; //全部
    public static final int TYPE_GET_GOODS = 1; //收货入库
    public static final int TYPE_PROFIT = 2; //盘盈入库
    public static final int TYPE_RETURN = 4;//退货入库
    public static final int TYPE_OTHER = 0;//其他入库
    @BindView(R.id.billitem_recyclerview)
    LRecyclerView recyclerview;
    @BindView(R.id.ll_bill_list)
    LinearLayout loadingLayout;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    private int type;
    private int pageNum = 1;

    private ADA_WarehouseItem adapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private boolean isVisible = false;
    private boolean isFirst = true;
    private boolean isRefreshing = false;

    public static FRA_WarehouseItem newInstance(int type) {
        FRA_WarehouseItem fra = new FRA_WarehouseItem();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fra.setArguments(bundle);
        return fra;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isFirst) {
            isFirst = false;
            return;
        }
        if (isVisibleToUser) {
            isVisible = true;
            if (mPresenter == null)
                return;
            refreshLayout.autoRefresh();
        } else {
            isVisible = false;
        }

    }

    @Override
    protected void onUserVisible() {
//        mPresenter.getDatas(pageNum, type == TYPE_ALL ? "" : type + "", getInWarehouseStatus(), DateHolder.INSTANCE.getInWarehouseKey(),
//                StringUtils.isEmpty(DateHolder.INSTANCE.getInWarehouseKey()) ? AppDateUtil.getTimeStamp(DateHolder.INSTANCE.getWarehouseDate(), AppDateUtil.YYYY_MM_DD) : "");
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        type = extras.getInt("type", -1);
    }

    @Override
    protected void initEventAndData() {

        mPresenter.initEvent();
//        mPresenter.getDatas(pageNum, type == TYPE_ALL ? "" : type + "", getInWarehouseStatus(), DateHolder.INSTANCE.getInWarehouseKey(),
//                StringUtils.isEmpty(DateHolder.INSTANCE.getInWarehouseKey()) ? AppDateUtil.getTimeStamp(DateHolder.INSTANCE.getWarehouseDate(), AppDateUtil.YYYY_MM_DD) : "");

        final GridLayoutManager manager = new GridLayoutManager(mContext, 4);
        recyclerview.setLayoutManager(manager);


        adapter = new ADA_WarehouseItem(mContext);
        adapter.setOnCardClickListener(this);

        lRecyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        recyclerview.setAdapter(lRecyclerViewAdapter);

        recyclerview.setPullRefreshEnabled(false);

        refreshLayout.setOnRefreshListener(this);

        recyclerview.setOnLoadMoreListener(this);

        refreshLayout.autoRefresh();

    }

    private String getInWarehouseStatus() {
        return "0";
    }

    @Override
    protected int getLayout() {
        return R.layout.fra_inwarehouse_item;
    }

    @Override
    protected View getTargetView() {
        return loadingLayout;
    }


    @Override
    public void showError(String msg) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        toggleShowEmpty(true, R.mipmap.ic_bill_empty, "暂无入库单", "");
    }


    @Override
    public void showNetError() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        showNetErrorEmpty(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FRA_WarehouseItem.super.toggleCloseEmpty();
                refreshLayout.autoRefresh();
            }
        });
    }


    @Override
    public void showToast(String str) {
        PosToast.INSTANCE.makeShortToast(mContext, str);
    }


    @Override
    public void onDataReceived(PageListResponse<InWarehouseItemEntity> response) {
        toggleShowEmpty(false);
//        lRecyclerViewAdapter = new LRecyclerViewAdapter(adapter);
//        recyclerview.setAdapter(lRecyclerViewAdapter);
        adapter.update(response.getList(), pageNum == 1);
        lRecyclerViewAdapter.notifyDataSetChanged();
        if (isRefreshing) {
            isRefreshing = false;
            refreshLayout.finishRefresh();
        }
    }

    @Override
    public void noMore() {
        recyclerview.setNoMore(true);
    }

    @Override
    public void noData() {
        refreshLayout.finishRefresh();
        toggleShowEmpty(true, R.mipmap.ic_bill_empty, "暂无入库单", "你可以换个日期试试");
    }

    @Override
    public void searchFailed() {
        toggleShowEmpty(true, R.mipmap.ic_bill_empty, "没有找到任何内容", "你可以换个关键词试试~");
    }

    @Override
    public void onDateChanged(long date) {
        // 时间切换，所有子页面修改时间 重新请求
        if (!isVisible)
            return;
        pageNum = 1;
        recyclerview.setNoMore(false);
        refreshLayout.autoRefresh();
//        mPresenter.getDatas(pageNum, type == TYPE_ALL ? "" : type + "", getInWarehouseStatus(), DateHolder.INSTANCE.getInWarehouseKey(),
//                AppDateUtil.getTimeStamp(DateHolder.INSTANCE.getWarehouseDate(), AppDateUtil.YYYY_MM_DD));
    }

    @Override
    public void onSearchKeyCome(String key) {
        // 搜索，所有页面进行搜索
        if (!isVisible)
            return;
        recyclerview.setNoMore(false);
        refreshLayout.autoRefresh();
//        mPresenter.getDatas(pageNum, type == TYPE_ALL ? "" : type + "", getInWarehouseStatus(), key, "");
    }

    @Override
    public void onCardClick(String id) {
        //列表中条目被点击的事件
        ACT_WarehouseDetail.startAct(mContext, id);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        isRefreshing = true;
        pageNum = 1;
        recyclerview.setNoMore(false);
        mPresenter.getDatas(pageNum, type == TYPE_ALL ? "" : type + "", getInWarehouseStatus(), DateHolder.INSTANCE.getInWarehouseKey(),
                StringUtils.isEmpty(DateHolder.INSTANCE.getInWarehouseKey()) ? AppDateUtil.getTimeStamp(DateHolder.INSTANCE.getWarehouseDate(), AppDateUtil.YYYY_MM_DD) : "");
    }

    @Override
    public void onLoadMore() {
        pageNum++;
        mPresenter.getDatas(pageNum, type == TYPE_ALL ? "" : type + "", getInWarehouseStatus(), DateHolder.INSTANCE.getInWarehouseKey(),
                AppDateUtil.getTimeStamp(DateHolder.INSTANCE.getWarehouseDate(), AppDateUtil.YYYY_MM_DD));
    }

    public boolean isRefreshing() {
        return refreshLayout.getState() != RefreshState.None;
    }

}
